namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorAccumulationSwingIndex : BaseIndicator
    {
        private LineShape tlsAsi;
        private LineShape tlsMAsi;

        public IndicatorAccumulationSwingIndex()
        {
            base.IndType = IndicatorType.ASI;
            base.IndName = "ASI";
            base.IndDesc = "振动升降指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 6.0));
            base.SourceDic = new Dictionary<string, string>();
            base.SourceDic["Open"] = "";
            base.SourceDic["Close"] = "";
            base.SourceDic["High"] = "";
            base.SourceDic["Low"] = "";
            base.fields = new string[] { "ASI", "MASI" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsAsi) && (obj != this.tlsMAsi))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int i)
        {
            double num = 0.0;
            double num2 = Convert.ToDouble(base.Crab.DataSource.Rows[i][base.SourceDic["High"]]);
            double num3 = Convert.ToDouble(base.Crab.DataSource.Rows[i][base.SourceDic["Low"]]);
            double num4 = Convert.ToDouble(base.Crab.DataSource.Rows[i][base.SourceDic["Close"]]);
            double num5 = Convert.ToDouble(base.Crab.DataSource.Rows[i][base.SourceDic["Open"]]);
            double num6 = 0.0;
            double num7 = 0.0;
            double num8 = 0.0;
            if (i > 0)
            {
                num = Convert.ToDouble(base.Crab.DataSource.Rows[i - 1][this.tlsAsi.Name]);
                num6 = Convert.ToDouble(base.Crab.DataSource.Rows[i - 1][base.SourceDic["Close"]]);
                num7 = Convert.ToDouble(base.Crab.DataSource.Rows[i - 1][base.SourceDic["Low"]]);
                num8 = Convert.ToDouble(base.Crab.DataSource.Rows[i - 1][base.SourceDic["Open"]]);
            }
            else
            {
                return new double[2];
            }
            double num9 = Math.Abs((double) (num2 - num6));
            double num10 = Math.Abs((double) (num3 - num6));
            double num11 = Math.Abs((double) (num2 - num7));
            double num12 = Math.Abs((double) (num6 - num8));
            double num13 = num4 - num6;
            double num14 = num4 - num5;
            double num15 = num6 - num8;
            double num16 = (num13 + (num14 / 2.0)) + num15;
            double num17 = (num9 >= num10) ? num9 : num10;
            double num18 = 0.0;
            if ((num9 >= num10) && (num9 >= num11))
            {
                num18 = (num9 + (num10 / 2.0)) + (num12 / 4.0);
            }
            else if ((num10 >= num9) && (num10 >= num11))
            {
                num18 = (num10 + (num9 / 2.0)) + (num12 / 4.0);
            }
            else if ((num11 >= num9) && (num11 >= num10))
            {
                num18 = num11 + (num12 / 4.0);
            }
            double num19 = 0.0;
            if (num18 != 0.0)
            {
                num19 = (16.0 * (num16 / num18)) * num17;
            }
            double curValue = num + num19;
            double num21 = Common.CalcuteMovingAvg(i, (int) base.IndPeriod[0].ParamValue, this.tlsMAsi.Name, this.tlsAsi.Name, curValue, base.Crab.DataSource);
            return new double[] { curValue, num21 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.TlsAsi, this.TlsMAsi };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsAsi);
            base.Crab.DeleteShape(this.TlsMAsi);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TlsAsi.Name, numArray[0], r);
                        base.Crab.SetValue(this.tlsMAsi.Name, numArray[1], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsAsi = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsMAsi = base.Crab.AddLine(fields[1], panelID);
            base.SourceDic["Open"] = source[0];
            base.SourceDic["Close"] = source[1];
            base.SourceDic["High"] = source[2];
            base.SourceDic["Low"] = source[3];
            base.IndPeriod[0].ParamValue = periods[0];
        }

        public LineShape TlsAsi
        {
            get
            {
                return this.tlsAsi;
            }
            set
            {
                this.tlsAsi = value;
            }
        }

        public LineShape TlsMAsi
        {
            get
            {
                return this.tlsMAsi;
            }
            set
            {
                this.tlsMAsi = value;
            }
        }
    }
}

